package cn.xshi.iot.live.util;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.iot.live.config.Config;
import cn.xshi.iot.live.model.CameraEntity;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * @Desc 实时流封装
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Component
public class PlaybackUtil {

    @Resource
    Config config;

    /**
     * 封装回放推流结构
     * @param cameraEntity
     */
    public void doPlayback(CameraEntity cameraEntity){
        String rtsp = "";
        String rtmp = "";
        String ip = IpUtil.IpConvert(cameraEntity.getIp());
        String url = "";
        int port = cameraEntity.getPort()==null?554:cameraEntity.getPort();
        String endTime = null;
        String cameraCode = cameraEntity.getCameraCode();
        switch (cameraCode){
            case Constant.HK://海康
                endTime = null != cameraEntity.getEndTime()?formatPullTime(cameraEntity.getEndTime(),false):formatPullTime(new Date(),false);
                rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/Streaming/tracks/" + cameraEntity.getChannel() + "?starttime=" + formatPullTime(cameraEntity.getStartTime(),true)+ "&endtime=" + endTime;
                break;
            case Constant.DH://大华
                endTime = null != cameraEntity.getEndTime() ?formatTime(cameraEntity.getEndTime()) :formatTime(new Date());
                rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/cam/playback?channel="+cameraEntity.getChannel()+"&subtype=0&starttime="+ formatTime(cameraEntity.getStartTime())+ "&endtime=" +endTime;
                break;
            case Constant.YS://宇视
                throw new ExceptionUtil("回放暂不支持宇视 协议");//待开发
            case Constant.PL://TP-LINK（普联）
                throw new ExceptionUtil("回放暂不支持TP-LINK（普联）协议");//待开发
            default:
                break;
        }
        rtmp = "rtmp://" + IpUtil.IpConvert(config.getStreamNginxIp()) + ":" + config.getStreamNginxTcpPort() + "/live/" +cameraEntity.getToken();
        if (config.getIpExtra().equals("127.0.0.1")) {
            url = rtmp;
        } else {
            url = "rtmp://" + IpUtil.IpConvert(config.getIpExtra()) + ":" + config.getStreamNginxTcpPort() + "/live/"+ cameraEntity.getToken();
        }
        String httpUrl = config.getHttp()+"://" + IpUtil.IpConvert(config.getStreamNginxIp()) + ":"+config.getHttpPort() +"/live?port=" + config.getStreamNginxTcpPort() + "&app=live&stream=" + cameraEntity.getToken();//增强Http流
        cameraEntity.setRtsp(rtsp);
        cameraEntity.setRtmp(rtmp);
        cameraEntity.setUrl(url);
        cameraEntity.setHttpURL(httpUrl);
    }

    /**
     * 构造监控回放查询字段
     * @param date
     * @param start
     * @return
     */
    private String formatPullTime(Date date, boolean start){
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        if (start) {
            calendar.add(Calendar.SECOND, -10);
        } else {
            calendar.add(Calendar.SECOND, 10);
        }
        //海康威视取回放的时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd#HHmmss$");
        String ret = sdf.format(calendar.getTime());
        ret = ret.replace("#", "t");
        ret = ret.replace("$", "z");
        return ret;
    }

    /**
     * 构造监控回放查询字段
     * @param date
     * @return
     */
    private String formatTime(Date date){
        //海康威视取回放的时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String ret = sdf.format(date);
        return ret;
    }
}
